package cn.kongwg.es.service.impl;

import cn.kongwg.es.entity.JBook;
import cn.kongwg.es.service.JBookService;
import cn.kongwg.es.utils.HttpParseUtils;
import com.alibaba.fastjson.JSON;
import org.elasticsearch.action.bulk.BulkRequest;
import org.elasticsearch.action.bulk.BulkResponse;
import org.elasticsearch.action.index.IndexRequest;
import org.elasticsearch.action.search.SearchRequest;
import org.elasticsearch.action.search.SearchResponse;
import org.elasticsearch.client.RequestOptions;
import org.elasticsearch.client.RestHighLevelClient;
import org.elasticsearch.common.unit.TimeValue;
import org.elasticsearch.common.xcontent.XContentType;
import org.elasticsearch.index.query.QueryBuilders;
import org.elasticsearch.index.query.TermQueryBuilder;
import org.elasticsearch.search.SearchHit;
import org.elasticsearch.search.builder.SearchSourceBuilder;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.io.IOException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.concurrent.TimeUnit;

/**
 * @author kongwg
 * @create 2021-04-02 19:05
 */
@Service
public class JBookServiceImpl implements JBookService {

    @Autowired
    private RestHighLevelClient restHighLevelClient;

    @Autowired
    private HttpParseUtils httpParseUtils;


    @Override
    public boolean addBooksToEs(String keyword,String page) {
        try {
            List<JBook> books = httpParseUtils.parseJD(keyword,page);

            BulkRequest request = new BulkRequest();
            request.timeout("3m");
            for (int i = 0; i < books.size(); i++) {
                request.add(
                        new IndexRequest("jd_books")
                            .source(JSON.toJSONString(books.get(i)), XContentType.JSON)
                );

            }
            BulkResponse responses = restHighLevelClient.bulk(request, RequestOptions.DEFAULT);
            return !responses.hasFailures();

        } catch (IOException e) {
            e.printStackTrace();
        }

        return false;
    }

    @Override
    public List<Map<String,Object>> getBooks(String keyword) {

        SearchRequest request = new SearchRequest();
        SearchSourceBuilder sourceBuilder = new SearchSourceBuilder();

        TermQueryBuilder termQueryBuilder = QueryBuilders.termQuery("title", keyword);
        sourceBuilder.query(termQueryBuilder);
        sourceBuilder.timeout(new TimeValue(60, TimeUnit.SECONDS));

        request.source(sourceBuilder);

        List<Map<String,Object>> list =new ArrayList<>();
        Map<String, Object> map = new HashMap<>();
        SearchResponse response = null;
        try {
            response = restHighLevelClient.search(request, RequestOptions.DEFAULT);
        } catch (IOException e) {
            e.printStackTrace();
        }
        for (SearchHit hit : response.getHits().getHits()) {
            list.add(hit.getSourceAsMap());
            
        }
        return list;
    }
}
